<template>
  <div>
    <img :src="Avatar" alt="" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import Vue from 'vue'
import { Toast } from 'vant'
import Avatar from '@/assets/avatar.jpg'
Vue.use(Toast)
export default {
  data() {
    return {
      username: '',
      password: '',
      Avatar
    }
  },
  methods: {
    onSubmit(values) {
      let str = ''
      let f = ''
      for (const item in values) {
        str += f + item + '=' + values[item]
        f = '&'
      }
      this.$http.post('/users/login', str).then((res) => {
        /* console.log(res) */
        if (res.code === 1) {
          Toast.success(res.msg)
          // 跳转到我的页面
          this.$router.push('/my')
        }
        Toast.fail(res.msg)
      })
    }
  },
  created() {
    this.$store.commit('hideFooter')
  },
  beforeDestroy() {
    this.$store.commit('showFooter')
  }
}
</script>

<style lane='scss' scoped>
img {
  width: 100px;
  height: 100px;
  margin: 50px 100px;
}
</style>
